<?php

/**
 * 短信息控制器
 */
class MessageAction extends UserAction {

    public $model;

    public function _initialize() {
        parent::_initialize();
        $this->model = D("Message");
    }

    /**
     * 发送短信息 
     */
    public function send() {
        if (IS_POST) {
            $uid     = session('uid');
            $message = t($_REQUEST['message']);
            $title   = t($_REQUEST['title']);
            $to_uid  = format_int($_REQUEST['tuid']);
            //用户是否登录
            if (false == $uid)
                $this->error('您尚未登录');
            if (false == $title)
                $this->error('标题不能为空');
            if (false == $message)
                $this->error('发送信息不能为空');
            if (false == $to_uid)
                $this->error('发送对象不能为空');
            $data    = array(
                'from_uid'    => $uid,
                'to_uid'      => $to_uid,
                'title'       => $title,
                'content'     => $message,
                'is_read'     => 0,
                'create_time' => time(),
            );
            $res          = M('Message')->add($data);
            if ($res) {
                $this->success('发送成功', U('Message/sendbox'));
            } else {
                $this->error('发送失败');
            }
        } else {
            //发送会员列表
            $user = M('User')->where(array('is_active' => 1))->select();
            $this->assign('user', $user);
            $this->display();
        }
    }

    /**
     * 发件箱 
     */
    public function sendbox() {
        $uid   = session('uid');
        $model = $this->model;
        $map   = array(
            'm.from_uid' => $uid
        );
        //取得满足条件的记录数
        $count       = $model
                ->join('As m JOIN ' . C('DB_PREFIX') . 'user as u ON u.user_id = m.to_uid')
                ->where($map)
                ->count();
        if ($count > 0) {
            import("ORG.Util.Page");

            $limit = 8;
            $order = 'create_time DESC';
            //创建分页对象
            $p     = new Page($count, $limit);
            //分页查询数据

            $voList = $model
                    ->join('As m JOIN ' . C('DB_PREFIX') . 'user as u ON u.user_id = m.to_uid')
                    ->where($map)
                    ->order($order)
                    ->field('m.*, u.uname')
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页模版
            $TP     = '共有{recordcount}条信息&nbsp;{pageindex}/{pagecount}&nbsp;{first}{prev}&nbsp;{liststart}{list}{listend}&nbsp;{next}{last}';
            $p->SetPager('default', $TP, array("listlong"      => "6", "first"         => "首页", "last"          => "尾页", "prev"          => "上一页", "next"          => "下一页", "list"          => "*", "disabledclass" => ""));

            //分页显示
            $page = $p->show();
            //模板赋值显示
            $data = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 收件箱 
     */
    public function getbox() {
        $uid   = session('uid');
        $model = $this->model;
        $map   = array(
            'm.to_uid' => $uid
        );
        //取得满足条件的记录数
        $count     = $model
                ->join('As m JOIN ' . C('DB_PREFIX') . 'user as u ON u.user_id = m.from_uid')
                ->where($map)
                ->count();
        if ($count > 0) {
            import("ORG.Util.Page");

            $limit = 8;
            $order = 'create_time DESC';
            //创建分页对象
            $p     = new Page($count, $limit);
            //分页查询数据

            $voList = $model
                    ->join('As m JOIN ' . C('DB_PREFIX') . 'user as u ON u.user_id = m.from_uid')
                    ->where($map)
                    ->order($order)
                    ->field('m.*, u.uname')
                    ->limit($p->firstRow . ',' . $p->listRows)
                    ->select();
            //分页模版
            $TP     = '共有{recordcount}条信息&nbsp;{pageindex}/{pagecount}&nbsp;{first}{prev}&nbsp;{liststart}{list}{listend}&nbsp;{next}{last}';
            $p->SetPager('default', $TP, array("listlong"      => "6", "first"         => "首页", "last"          => "尾页", "prev"          => "上一页", "next"          => "下一页", "list"          => "*", "disabledclass" => ""));
            //分页显示
            $page           = $p->show();
            //模板赋值显示
            $data           = array(
                'list'      => $voList,
                'page'      => $page,
                'nowPage'   => $_REQUEST['page'],
                'totalPage' => round($count / $limit),
            );
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function delMessage() {
        if (empty($_POST['ids'])) {
            echo 0;
            exit;
        }
        $map = array();
        $map['message_id'] = array('in', t($_POST['ids']));
        $res = M('Message')->where($map)->delete();
        echo $res ? '1' : '0';
    }

    /**
     * 查看短信息 
     */
    public function read() {
        $messageID = format_int($_REQUEST['id']);
        $message   = $this->model->find($messageID);
        if ($message) {
            $message['is_read'] = 1;
            $this->model->save($message);
            $reData             = reStatus(true, '', $message['content']);
        } else {
            $reData = reStatus(false, '信息已删除');
        }
        $this->ajaxReturn($reData);
    }

}
